package com.lcy.redistest.jedispool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Objects;

/**
 * @author lichenyang
 * @create 2021-04-28 4:58 下午
 *
 * JedisPool工具类,用来创建单例 JedisPool
 */
public class JedisPoolUtil {

    private volatile static JedisPool jedisPool = null;

    public static JedisPool getJedisPool(String host, int port) {
        if(jedisPool == null){
            synchronized (JedisPoolUtil.class){
                if(jedisPool == null){
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxIdle(32);
                    poolConfig.setMinIdle(100 * 1000);
                    poolConfig.setTestOnBorrow(true);
                    jedisPool = new JedisPool(poolConfig, host, port);
                }
            }
        }
        return jedisPool;
    }

    /**
     * 释放当前连接
     * @param jedisPool
     * @param jedis
     */
    public static void release(JedisPool jedisPool, Jedis jedis){
        if(Objects.nonNull(jedis)){
            jedisPool.returnResourceObject(jedis);
        }

    }


}
